<template>

    <div class="head">
      <div class="box">
        <span style="color:dimgray; margin-left: 15px;" @click="toCity" >{{ store.cityName }}</span>
        <img src="../assets/zt.png" alt="" style="width: 6px;height: 3px;position: absolute;top: 25px;left: 55px;" >
        <span style="font-size: 18px;">电影</span>
       
      </div>

        <div class="nav">
       <span :class="{'redClass':isColor ===1}" @click="clickOn">正在热映</span>
       <span ref="navS" :class="{'redClass':isColor ===0}" @click="clickSp">即将上映</span>
      </div>
    
      <div class="bottom" :style="bottomStyle">
      </div>

      <Main></Main>
        
    </div>
</template>

<script setup>
import {   computed, inject, onMounted, provide, ref } from 'vue';
import Main from "../view/Main.vue"
import listStore from "../store/index"
import axios from 'axios';
import router from "../router/index"

 const isColor = ref(1);
 const className =  ref("")
 const type = ref(1)
 const bottomPosition = ref(0)
 const navS = ref(null);
 provide("type",type)

// 引入store
const store = listStore()

onMounted(()=>{
  store.toListOrCinemas = true
})
   // 修改类名的方法
   const updateClassName = () => {
      className.value = 'redClass';
    };


// 计算横线滑动
const bottomStyle = computed(()=>{
    return {
        transform: `translateX(${bottomPosition.value}px)`
    }
})
 // 数据懒加载  方法
const onLoad = async() => {  
    
    store.finished = false  //必须重置为false 否则数据加载不了不执行onload  必须写再onload方法里
      if(store.total === store.mainDataList.length && store.total !== 0) {
      

         // 数据全部加载完成
         console.log("数据取完");
         console.log("到底了");
       store.finished = true
       store.pageNum = 1
        return
      }
     
    //   store.getList(store.pageNum++,type.value)
      store.pageNum++
        const res =  await axios({  
            url:`https://m.maizuo.com/gateway?cityId=${store.cityId}&pageNum=${store.pageNum}&pageSize=10&type=${type.value}&k=4231560`,
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993","bc":"110100"}',
                'X-Host':'mall.film-ticket.film.list'
            }
    })
    console.log(store.total,"....",store.mainDataList.length);
     
    store.mainDataList = [...store.mainDataList,...res.data.data.films]
     // 加载状态结束
     store.loading = false;
      
    };
// 正在热映
const clickOn = () =>{
    
    isColor.value = 1;
    className.value = ''
    bottomPosition.value = 0
    type.value = 1;
    store.pageNum = 1
    store.total = 0  // 必须重置为0 否则load事件不生效
    store.getList(store.cityId,store.pageNum,type.value)
     // 重新onload调用事件 实现数据滚动刷新
     onLoad()
   
}

// 即将上映
 const clickSp = ()=>{
    isColor.value = 0;
    type.value = 2
    updateClassName();
    store.pageNum = 1
    store.getList(store.cityId,store.pageNum,type.value)

    // 计算
    bottomPosition.value =navS.value.offsetLeft - 50  ;
 }

 // 点击北京回调函数
 const toCity = ()=>{
    router.push("/city")
 }
</script>

<style scoped>
.box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 5px;
    width: 50vw;
    height: 5vh;
}
.nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    /* margin: auto; */
    margin-top: 25px;
    margin-left: 50px;
    background-color: white;
    width: 70vw;
    height: 30px;
 
}
.nav span {
    display: inline-block;
    height: 20px;
    line-height: 20px;
}
.redClass {
    color: #ff5f16;
}
.bottom {
margin-left: 50px;
margin-top: 10px;
  height: 2px;
  width: 60px;
  background-color: #ff5f16;
  transition: transform 0.4s ease;
}
</style>